package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.Group;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.HouseNeedDataContract;
import com.shd.edanpei.presenter.HouseNeedDataPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 住房订单 订单所需材料2
 */

public class HouseNeedDataActivity2 extends BaseActivity<HouseNeedDataPresenter> implements HouseNeedDataContract.HouseNeedDataIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_rate)
    TextView tvRate;
    //    @BindView(R.id.et_repayWay)
//    EditText etRepayWay;
    @BindView(R.id.tv_repayWay)
    TextView tvRepayWay;
    @BindView(R.id.tv_repayDate)
    TextView tvRepayDate;
    @BindView(R.id.radio_group_otherAsset)
    RadioGroup radioGroupOtherAsset;
    @BindView(R.id.radio_group_company)
    RadioGroup radioGroupCompany;
    @BindView(R.id.group_company)
    Group groupCompany;
//    @BindView(R.id.radio_group_site)
//    RadioGroup radioGroupSite;
//    @BindView(R.id.rl_siteSize)
//    View siteSize;
    @BindView(R.id.et_companyStake)
    EditText etCompanyStake;
    @BindView(R.id.tv_operationState)
    TextView tvOperationState;
    @BindView(R.id.tv_operationTerm)
    TextView tvOperationTerm;
    //    @BindView(R.id.et_liabilities)
//    EditText etLiabilities;
//    @BindView(R.id.et_siteSize)
//    EditText etSiteSize;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_done)
    TextView tvDone;

    private int from;

    @Override
    protected int setLayout() {
        return R.layout.activity_houseneeddata2;
    }

    @Override
    protected HouseNeedDataPresenter bindPresenter() {
        return new HouseNeedDataPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("订单所需材料");
        Bundle extras = getIntent().getExtras();
        CustomerBean orderBean = extras.getParcelable(Constant.DATA);
        from = extras.getInt(Constant.FROM);
        mPresenter.setCustomerBean(orderBean);
        if (from == 1) {
            tvDone.setText("确认修改");
            headline.setText("修改订单所需材料");
        }
    }

    @Override
    protected void initListener() {
        radioGroupOtherAsset.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                mPresenter.setOtherAsset(i == R.id.radio_button_yes);
            }
        });
        radioGroupCompany.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                boolean isShow = i == R.id.radio_button_yes;
                mPresenter.setCompany(isShow);
                groupCompany.setVisibility(isShow ? View.VISIBLE : View.GONE);
            }
        });
//        radioGroupSite.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int i) {
//                boolean isShow = i == R.id.radio_button_yes;
//                mPresenter.setSite(isShow);
//                siteSize.setVisibility(isShow ? View.VISIBLE : View.GONE);
//            }
//        });
        etCompanyStake.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setCompanyStake(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
//        etSiteSize.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
//                mPresenter.setSiteSize(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
        etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setRemark(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
//        etRepayWay.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                mPresenter.setRepayWay(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    /**
     * 选择订单金额
     */
    @OnClick(R.id.rl_amount)
    void onAmount() {
        mPresenter.setAmount(tvAmount);
    }

    /**
     * 预期比
     */
    @OnClick(R.id.rl_rate)
    void onRate() {
        mPresenter.setRate(tvRate);
    }

    /**
     * 归还方式
     */
    @OnClick(R.id.ll_repayWay)
    void onRepayWay() {
        mPresenter.setRepayWay(tvRepayWay);
    }


    /**
     * 订单期限
     */
    @OnClick(R.id.rl_repayDate)
    void onRepayDate() {
        mPresenter.setRepayDate(tvRepayDate);
    }

    /**
     * 选择经营状况
     */
    @OnClick(R.id.ll_operationState)
    void onOperationState() {
        mPresenter.setOperationState(tvOperationState);
    }

    /**
     * 选择经营年限
     */
    @OnClick(R.id.ll_operationTerm)
    void onOperationTerm() {
        mPresenter.setOperationTerm(tvOperationTerm);
    }


    @OnClick(R.id.tv_done)
    void onDone() {
        if (from == 1) {
            mPresenter.update();
        } else {
            mPresenter.done();
        }
    }

    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(ContextCompat.getColor(this, R.color.black));
    }

    @Override
    public void setData(CustomerBean bean) {
        if (!TextUtils.isEmpty(bean.getApplyAmount())) setText(tvAmount, bean.getApplyAmount());
        if (!TextUtils.isEmpty(bean.getApplyInterest())) setText(tvRate, bean.getApplyInterest());
        if (!TextUtils.isEmpty(bean.getApplyRepaymentType()))setText(tvRepayWay, bean.getApplyRepaymentType());
        if (!TextUtils.isEmpty(bean.getApplyTerm()))setText(tvRepayDate, bean.getApplyTerm());
        radioGroupOtherAsset.check(bean.isHaveOtherHouse() ? R.id.radio_button_yes : R.id.radio_button_no);
        radioGroupCompany.check(bean.isHaveCompany() ? R.id.radio_button_yes : R.id.radio_button_no);
        groupCompany.setVisibility(bean.isHaveCompany() ? View.VISIBLE : View.GONE);
        if (bean.isHaveCompany()) {
            setText(etCompanyStake, bean.getCompanyShare());
            setText(tvOperationState, bean.getOperationState());
            setText(tvOperationTerm, bean.getOperationTerm());
        }
//        radioGroupSite.check(bean.isHaveField() ? R.id.radio_button_yes : R.id.radio_button_no);
//        if (bean.isHaveField()) {
//            setText(etSiteSize, bean.getFieldArea());
//        }
        if (!TextUtils.isEmpty(bean.getRemark()))setText(etRemark, bean.getRemark());
    }

}
